<template>
  <div id="bottomDiv" class="control-box">
    <el-progress :percentage="percent" color="blue" status="exception"></el-progress>

    <div class="control-content">
      <div class="content-extend"><slot name="extend-left"></slot></div>
      <div class="file-pagination-container">
        <el-button
          :disabled="currentIndex === files.length - 1"
          type="primary"
          size="mini"
          @click="handleAutoPlay"
          :icon="playStarted ? 'el-icon-video-pause' : 'el-icon-video-play'"
          circle
        />

        <el-button
          type="primary"
          size="mini"
          :disabled="loading || currentIndex <= 0"
          @click="prevFrame"
          icon="el-icon-back"
          circle
        />

        <span> 当前：( {{ currentIndex + 1 }} / {{ files.length }} ) </span>

        <el-button
          type="primary"
          size="mini"
          :disabled="loading || currentIndex >= files.length - 1"
          @click="nextFrame"
          icon="el-icon-right"
          circle
        />

        <el-select size="mini" :value="currentIndex" @change="changePage" style="width: 80px">
          <el-option v-for="(item, index) in files" :key="item.fileId" :label="index + 1" :value="index" />
        </el-select>
      </div>

      <div class="content-extend"><slot name="extend-right"></slot></div>
    </div>
  </div>
</template>

<script>
const AsyncBeforeChange = (func) => {
  if (func instanceof Promise) return func

  return new Promise((resolve, reject) => {
    const valid = func
    valid ? resolve(valid) : reject(valid)
  })
}

export default {
  name: 'FileListPagination',

  props: {
    files: {
      type: Array,
      default: () => [],
    },
    interval: {
      type: Number,
      default: 1500,
    },
    changeBefore: {
      type: Function,
      default: (index, current) => {
        console.log('这是默认的函数')
        return true
      },
    },
    // 需要接收当前的字段值
    value: {
      type: Number,
      default: 0,
    },

    options: {
      type: Object,
      default: () => ({
        prev: true,
        next: true,
        current: true,
        total: true,
        jump: true,
        multi: true,
      }),
    },

    percent: {
      type: Number,
      default: 0,
    },
  },

  data() {
    return {
      // current: {},
      currentIndex: -1,

      // 快进/快退的步长,TODO:配置界面
      step: 1,

      loading: false,

      // 自动播放
      playStarted: false,
    }
  },

  created() {},
  beforeDestroy() {
    if (this.timer) {
      window.clearInterval(this.timer)
    }
  },
  model: {
    prop: 'value',
    event: 'change',
  },
  watch: {
    currentIndex: {
      immediate: false,
      handler(val) {
        const current = this.files[val]
        this.$emit('change', val, current)
      },
    },
    value: {
      immediate: true,
      handler(val) {
        this.changePage(val)
      },
    },
  },
  methods: {
    changePage(value) {
      const { files, currentIndex } = this
      if (value < 0 || value >= this.files.length || value === currentIndex) {
        return
      }

      const newVal = value
      AsyncBeforeChange(this.changeBefore(newVal, files[newVal]))
        .then((res) => {
          res && (this.currentIndex = newVal)
        })
        .catch(() => {})
    },

    prevFrame() {
      const { currentIndex, step, files } = this
      const newVal = Math.max(currentIndex - step, 0)

      AsyncBeforeChange(this.changeBefore(newVal, files[newVal]))
        .then((res) => {
          res && (this.currentIndex = newVal)
        })
        .catch(() => {})
    },

    nextFrame() {
      const { currentIndex, step, files, timer } = this
      if (currentIndex === files.length - 1 && timer) {
        this.handleAutoPlay()
      }

      const newVal = Math.min(currentIndex + step, files.length - 1)
      AsyncBeforeChange(this.changeBefore(newVal, files[newVal]))
        .then((res) => {
          res && (this.currentIndex = newVal)
        })
        .catch(() => {})
    },

    handleAutoPlay() {
      // console.log(this.timer)
      if (this.timer) {
        window.clearInterval(this.timer)
        this.playStarted = false
        this.timer = null
      } else {
        this.timer = setInterval(() => {
          this.nextFrame()
        }, this.interval)
        this.playStarted = true
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.control-box {
  line-height: 36px;
  padding: 7px 10px;
  background: #fff;
  .control-content {
    display: flex;
    justify-content: space-between;
    flex-wrap: nowrap;
    word-wrap: break-word;
    word-break: break-all;

    :first-child {
      text-align: left;
    }
    :last-child {
      text-align: right;
    }
    .content-extend {
      flex: 1;
    }
    .file-pagination-container {
      text-align: center;
      flex: 1;
      white-space: nowrap;
      overflow: hidden;
    }
  }
}
</style>
